package com.YOUtils.Base.language;

import com.YOUtils.Base.HttpUtils;
import com.YOUtils.Base.Str.YoStringUtils;
import com.YOUtils.YException.YNullPointerException;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.internal.LinkedTreeMap;
import com.yum.Iterator.Map.YAbstractMap.HashMap.YHashMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2023/6/25
 * 翻译工具类
 * @Time 15:40
 * @Program YOUilt
 * @Author YoHaNe/XiaoShuai
 */
public class TranslateLanguage {

    private static final String TO_EN = "http://fanyi.youdao.com/translate?=&doctype=json&type=ZH_CN2EN&i=";

    private static final String TO_CN = "http://fanyi.youdao.com/translate?=&doctype=json&type=EN2ZH_CN&i=";

    public static String toEn(String str){
        return toEnOrCh(str,0);
    }

    public static String toCn(String str){
        return toEnOrCh(str,1);
    }

    @SuppressWarnings("all")
    public static String toEnOrCh(String str,int ce){
        if(YoStringUtils.isEmpty(str))
            throw new YNullPointerException();
        String resultJson = ce == 0 ?
                HttpUtils.doGet(TO_EN + str) : HttpUtils.doGet(TO_CN + str);
        HashMap json = new Gson().fromJson(resultJson,HashMap.class);
        if(json.get("errorCode").equals(0.0)){
            List<Object> a = (List<Object>)json.get("translateResult");
            List<Object> b = (List<Object>) a.get(0);
            Map<String,String> map = (LinkedTreeMap<String, String>) b.get(0);
            return map.get("tgt");
        }
        return null;
    }

}
